package br.ufpb.di.ppgi.apresentacao;

import java.io.File;

import javax.annotation.PostConstruct;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;

import br.com.caelum.vraptor.ioc.ApplicationScoped;
import br.com.caelum.vraptor.ioc.Component;
import br.ufpb.di.ppgi.negocio.modelo.contato.InternetIdentity;
import br.ufpb.di.ppgi.util.PropertiesUtil;

@Component
@ApplicationScoped
public class SiteConfiguration {
	private final PropertiesUtil properties;
	private final ServletContext servletContext;
	private static SiteConfiguration instance;
	
	public static SiteConfiguration getInstance() {
		return instance;
	}
	
	public SiteConfiguration(PropertiesUtil properties, ServletContext servletContext) {
		this.properties = properties;
		this.servletContext = servletContext;
	}
	
	@PostConstruct
	public void configureInstance() {
		instance = this;
	}
	
	public String getContextDirPath() {
		return servletContext.getRealPath("/");
	}
	
	public String getContextPath() {
		return servletContext.getContextPath();
	}
	
	private String anexosDirPath; 
	
	public String getAnexosDirPath() {
		if (anexosDirPath == null) {
			synchronized (this) {
				if (anexosDirPath == null) {
					anexosDirPath = buildAnexosDirPath();
				}
			}
		}
		return anexosDirPath;
	}
	
	private String buildAnexosDirPath() {
		String dirPath = getContextDirPath();
		String downloadConfPath = properties.getString("path.to.anexos");
		dirPath = dirPath + downloadConfPath.replace('/', File.separatorChar);
		dirPath = dirPath.endsWith(File.separator) ? dirPath : dirPath + File.separator; 
		return dirPath;
	}
	
	public String getAnexosUrlRegex() {
		return properties.getString("path.to.anexos.url");
	}

	public SimpleEmail getSimpleEmail() throws EmailException {
		SimpleEmail simpleEmail = new SimpleEmail();

		simpleEmail.setAuthentication(properties.getString("mail.smtp.user"),properties.getString("mail.smtp.pass"));		
		
		
		simpleEmail.setHostName(properties.getString("mail.smtp.host"));
		simpleEmail.setSmtpPort(Integer.valueOf(properties.getString("mail.smtp.port")));
		simpleEmail.setCharset(getEmailDefaultCharset());
		simpleEmail.setSSL(Boolean.valueOf(properties.getString("mail.smtp.SSL")));
		simpleEmail.setTLS(Boolean.valueOf(properties.getString("mail.smtp.TLS")));
		simpleEmail.setDebug(Boolean.valueOf(properties.getString("mail.smtp.debug")));

		return simpleEmail;
	}
	
	public InternetIdentity getDe() {
		return new InternetIdentity(properties.getString("mail.smtp.user"),properties.getString("mail.smtp.name"));
	}
	
	public String getEmailDefaultCharset() {
		return properties.getString("mail.charset");
	}

	public InternetIdentity getInetIdSecretario() {
		return new InternetIdentity(properties.getString("model.email.secretario"), "Secretario PPGI");
	}

	public InternetIdentity getInetIdCoordenador() {
		return new InternetIdentity(properties.getString("model.email.coordenador"), "Coordenador PPGI");
	}

	public String getJasperDirPath() {
		String jasperDir = properties.getString("path.to.jasper");

		if (!jasperDir.endsWith("/"))
			jasperDir = jasperDir.concat("/");

		jasperDir = jasperDir.startsWith("/") ? jasperDir : getContextDirPath().concat(jasperDir);

		return jasperDir;
	}
	
	
	//---------------------------------------------------------------------------------------------- UTILZ
	private static final String BASE_PORT = "http://%s:%d%s";
	private static final String BASE = "http://%s%s";
	
	public static String getBase(HttpServletRequest request) {
		int port = request.getServerPort();
		String base; 
		base = (port != 80) ?
			String.format(BASE_PORT, request.getServerName(),request.getServerPort(),request.getContextPath())
			: String.format(BASE, request.getServerName(),request.getContextPath());
		base = base.endsWith("/") ? base : base + "/";
		return base;
	}
}
